<?php 
class Receipt extends CI_Controller {
	
	function receipt()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->library('fpdf');
		$this->load->library('enc');
                $this->load->model('mmaster');
                $this->load->model('mglobal');
				$this->load->model('mreceipt');
		$this->is_logged_in();
		#$this->mjadwal->saveLog();
		$this->load->helper('url');
        $this->load->library('enc');

	}
	
	function is_logged_in()
	{
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			redirect(base_url() .'index.php/login');
		}		
	}
	

	function index($lastId= "")
	{
		$data['lastId'] = $lastId;
		$data['template'] 	= "shell/smooth";
		$data['main_view']  = 'f-receipt';
		$this->load->view($data['template'],$data);
	}

	//popup window, berisi daftar pilihan iccid tambahan berbentuk checkbox
	function newcheckbox()
	{
		$this->load->view('newcheckbox');
	}
	
	//proses pembuatan form receipt 
	function newreceipt($proses = "", $id = "")
    {
        $data['template'] = "shell/smooth";
        $data['main_view'] = "f-receipt";
        $data['jenis'] = "create";
        $data['proses'] = $proses;
        $data['id'] = $id;	
		$data['salesID'] = $this->mreceipt->getSalesID($this->session->userdata('username'));

		// print_r($data['salesID']);die();
		
		$res = $this->mreceipt->getClusterName();	
		if (count($res) > 0)
			foreach($res as $result)
			{
				$data['ter_name'] = $result['territory_name'];
				$ter_id = $result['territory_id'];
			}
		else
		{
			$data['ter_name'] = "no data";
			$ter_id = "no data";
		}
		
        #proses save data        
        $this->form_validation->set_rules('receipt_date', 'receipt date', 'required');
        $this->form_validation->set_rules('sales_id', 'sales id', 'required');
        $this->form_validation->set_rules('iccid[]', 'iccid', 'required');
        $this->form_validation->set_rules('item_code[]', 'kode item', 'required');
        $this->form_validation->set_rules('item_group_name[]', 'nama group item', 'required');
        $this->form_validation->set_rules('item_name[]', 'nama item', 'required');
        
		
		if ($this->form_validation->run() != false) {
		
			 // $iccid is the array you want to check for duplicates		
			$iccid = $this->input->post('iccid');
			$has_duplicates = (count(array_unique($iccid)) != count($iccid));
			
			if ($has_duplicates) {
			   // do something
			   $this->session->set_flashdata('message', 'Terdapat duplikat iccid');
			   redirect(base_url() . "index.php/receipt/newreceipt");
			}
			else
			{		
				// masukkan data di sini
				$lastRid = $this->mreceipt->saveReceipt($ter_id);
				$count = 0;
			
				for($i = 0; $i < sizeof($iccid); $i++) 
				{
					$this->mreceipt->saveReceiptDetail($lastRid, $i);   
					$count++;
				}
				//echo $count;
				$this->session->set_flashdata('message', 'Data Berhasil dimasukkan');
				redirect(base_url() . "index.php/receipt/index/" . $lastRid );
			}
		}
		
        $hasil = "";
        $this->load->view($data['template'], $data);
    }
	
	
    // untuk manage data yang ditampilkan pada grid view
    public function read() {

    	//edited by Alfin Akhret
		if($this->session->userdata('is_self_service') == 1)
		{
			$result = $this->mreceipt->getAllReceiptCurrentUser();
		}
		else
		{
			$result = $this->mreceipt->getAllReceipt();
		}
    	
    
    	//encode
        //encode the sell_in_id
    	for($i=0; $i<count($result); $i++)
        {
       		$result[$i]->territory_id = $this->enc->fogit($result[$i]->territory_id);
            $result[$i]->receipt_id = $this->enc->fogit($result[$i]->receipt_id);
        }

		echo json_encode($result);
	}
	

	// added by: alfin akhret
	function defoger()
    {
    	//decode
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }

	//process posted form data (the requested items like province)
    function load_sales($sales_id){
        
        $data['test'] = $this->mreceipt->getSales($sales_id); //Search DB
        
        if('IS_AJAX')
        {
            echo json_encode($data['test']);
        }
    }
	
	//concate id pada row table tertentu untuk keperluan view
	public function concateId($id)
	{
		$tempId = "" . $id;
		if (strlen($tempId) < 8)
		{
			for($ii=strlen($tempId); $ii<8; $ii++)
			{
				$tempId = '0' . $tempId;
			}
		}
		return $tempId;
	}
	
	//proses data untuk halaman detail
	function getDetail($id1='', $id2='')
	{
		$rcp_id = $this->enc->defog($id1); // added by Alfin Akhret
		$ter_id = $id2;	
		
		$temp = $this->mreceipt->getClusterName();	
		if (count($temp) > 0)
			foreach($temp as $result)
			{
				$data['ter_name'] = $result['territory_name'];
				$ter_id = $result['territory_id'];
			}
		else
		{
			$data['ter_name'] = "No Data";
			$ter_id = "No Data";
		}
		
		$list = $this->pdfAndDetail($rcp_id, $ter_id);
		$data['strId'] = $list[0];
		$data['dist'] = $list[1];
		$data['receipt'] = $list[2];
		$data['sales_name'] = $list[3];
		$data['receipt_detail'] = $list[4];
		
		for($ii=0;$ii<count($list[4]);$ii++)	
		{
			$data['itemDetail'][$ii] = $list[5+$ii];
		}
		
		$data['template'] 	= "shell/smooth";
		$data['main_view']  = 'f-receipt-detail';
		$this->load->view($data['template'],$data);
		
	
	}
	
	//proses data untuk men-generate halaman pdf dan detail
	function pdfAndDetail($rcp_id, $ter_id)
	{
		$list = array();
		
		$list[0] = $this->concateId($rcp_id);
		
		$this->db->select('distributor_name, distributor_address');
		$this->db->where('territory_id', $ter_id);
		$q = $this->db->get('t_mtr_distributor');
		$list[1] = $q->result_array();		
		
		$list[2] = $this->mreceipt->getById($rcp_id);
		
		$this->db->select('user_name');
		$this->db->where('user_id', $list[2]->sales_id);
		$q = $this->db->get('t_mtr_user');
		$list[3] = $q->result_array();		
		
		$this->db->select('*');
		$this->db->where('receipt_id', $rcp_id);
		$q = $this->db->get('t_trx_receipt_detail');
		$list[4] = $q->result_array();
		
		for($ii=0;$ii<count($list[4]);$ii++)
		{
			$list[$ii+5] = $this->mreceipt->getItemDetail($list[4][$ii]['iccid']);
		}
		
		return $list;
	
	}
	


	//proses data untuk halaman pdf
	function topdf($id1='', $id2='')
	{
	
		$rcp_id = $this->enc->defog($id1);
		$ter_id = $id2;	
		
		$temp = $this->mreceipt->getClusterName();	
		if (count($temp) > 0)
			foreach($temp as $result)
			{
				$ter_name = $result['territory_name'];
				$ter_id  = $result['territory_id'];
			}
		else
		{
			$ter_name = "No Data";
			$ter_id  = "No Data";
		}
		
		$list = $this->pdfAndDetail($rcp_id, $ter_id);
		$strId = $list[0];
		$dist = $list[1];
		$receipt = $list[2];
		$sales_name = $list[3];
		// $receipt_detail = $list[4];
		$receipt_detail = $this->mreceipt->getPdfDetails($rcp_id);   

		for($ii=0;$ii<count($list[4]);$ii++)	
		{
			$itemDetail[$ii] = $list[5+$ii];
		}
		
		//begin configuration
		$textColour = array( 0, 0, 0 );
		$headerColour = array( 100, 100, 100 );
		$tableHeaderTopTextColour = array( 0, 0, 0 );
		$tableHeaderTopFillColour = array( 250, 250, 250 );
		$tableHeaderTopProductTextColour = array( 0, 0, 0 );
		$tableHeaderTopProductFillColour = array( 143, 173, 204 );
		$tableHeaderLeftTextColour = array( 99, 42, 57 );
		$tableHeaderLeftFillColour = array( 184, 207, 229 );
		$tableBorderColour = array( 50, 50, 50 );
		$tableRowFillColour = array( 213, 170, 170 );
		

		$chartColours = array(
						  array( 255, 100, 100 ),
						  array( 100, 255, 100 ),
						  array( 100, 100, 255 ),
						  array( 255, 255, 100 ),
						);

		$data = array(
				  array( 9940, 10100, 9490, 11730 ),
				  array( 19310, 21140, 20560, 22590 ),
				  array( 25110, 26260, 25210, 28370 ),
				  array( 27650, 24550, 30040, 31980 ),
				);
		// End configuration
		
		
		$this->load->helper('path');

		$font_directory = './fpdf_fonts/';
		set_realpath($font_directory);
		define('FPDF_FONTPATH',$font_directory);
		
		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 5);
		$this->fpdf->SetMargins(10, 20, 20);
		$this->fpdf->AliasNbPages();
		
		
		//header
		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png',10,3,50,0,'');			
		$this->fpdf->SetFont('Arial','B',16);
		//$this->fpdf->Cell(80);
		$this->fpdf->Cell(175,-5,"Receipt Notes",0,1,'R');	
		$this->fpdf->Ln( 10 );		
		$this->fpdf->SetFont('Arial','',10);
		$this->fpdf->Cell(180,0,"(Pengembalian Barang Sales)",0,1,'R');		
		$this->fpdf->Line(10, 20, 200, 20);
		
		//table
		$this->fpdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
		
		//Cluster Information		
		$this->fpdf->Ln(5);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(19, $ter_name);
		$this->fpdf->Ln( 6 );
		// $this->fpdf->SetFont('Helvetica','',10);
		// $this->fpdf->Write(19, $dist[0]['distributor_address']);		
		
		
		// Date and receipt no
		$this->fpdf->Ln(15);
		$this->fpdf->SetXY(130, 42);
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->Cell( 25, 8, "Tanggal", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "No. Receipt", 1, 0, 'C', true );		
		//$this->fpdf->Ln( 8 );
		$this->fpdf->SetXY(130, 50);
		
		$fill = false;
		$row = 0;		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->SetFont( 'Arial', '', 8 );		
		$this->fpdf->Cell( 25, 8, date("d-m-Y", strtotime($receipt->receipt_date)), 1, 0, 'C', $fill );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 45, 8, $strId, 1, 0, 'C', $fill );
					
						
	
		$this->fpdf->Ln( 20 );
			
		// Order Information
		$this->fpdf->SetXY(10, 42);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "ID Sales", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Nama Sales", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $receipt->sales_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $sales_name[0]['user_name'], 1, 0, 'C', $fill );		

		
		// Order Information
		$this->fpdf->Ln( 20 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 10, 8, "No", 1, 0, 'C', true );
		$this->fpdf->Cell( 30, 8, "Kode barang", 1, 0, 'C', true );		
		$this->fpdf->Cell( 20, 8, "Jumlah", 1, 0, 'C', true );		
		$this->fpdf->Cell( 80, 8, "Nama Barang", 1, 0, 'L', true );		
		$this->fpdf->Cell( 50, 8, "Keterangan", 1, 0, 'L', true );			
		$this->fpdf->SetFont( 'Arial', '', 7 );
		$this->fpdf->Ln( 1 );
		
		$subname = "";
		// if (count($receipt_detail) > 0)
		// {
		// 	for($jj=0; $jj<count($receipt_detail); $jj++)
		// 	{
		// 		$this->fpdf->Ln( 7 );
		// 		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		// 		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		// 		//$this->fpdf->Cell( 40, 7, $receipt_detail[$jj]['iccid'], 1, 0, 'L', $fill );
		// 		//$this->fpdf->Cell( 25, 7, $itemDetail[$jj][0]['mdn'], 1, 0, 'L', $fill );		
		// 		$this->fpdf->Cell( 30, 7, $itemDetail[$jj][0]['item_code'], 1, 0, 'L', $fill );		
		// 		$this->fpdf->Cell( 25, 7, $itemDetail[$jj][0]['item_group_name'], 1, 0, 'L', $fill );
		// 		$subname = substr($itemDetail[$jj][0]['item_name'], 0 , 40);					
		// 		$this->fpdf->Cell( 70, 7,  $subname, 1, 0, 'L', $fill );					
				
		// 		if ($this->fpdf->GetY() >= 267)
		// 		{
		// 			$this->fpdf->SetY(-15); 
		// 			// $this->fpdf->Cell(100, 8, "Anuanu", 1, 0, 'C', true );
		// 			$this->fpdf->Cell(0, 8, "~~ Printed by " . $this->session->userdata('username') . " ~~", 0, 0, 'C' );
		// 			//Arial italic 8 

		// 			$this->fpdf->SetFont('Arial','I',8); 
		// 			//Page number 
		// 			$this->fpdf->Cell(10,8,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'R'); 
	
		// 		}
				
		// 	}
		// }	
		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$nomor = 1;
		foreach ($receipt_detail as $detail) {
			$this->fpdf->Ln( 7 );
			$this->fpdf->Cell( 10, 7, $nomor, 1, 0, 'C', false );
			$this->fpdf->Cell( 30, 7, $detail->item_code, 1, 0, 'C', false );		
			$this->fpdf->Cell( 20, 7, $detail->jumlah, 1, 0, 'C', false );		
			$this->fpdf->Cell( 80, 7, $detail->item_name, 1, 0, 'L', false );		
			$this->fpdf->Cell( 50, 7, $detail->item_group_name, 1, 0, 'L', false );
			$nomor++;
		}	

		//Signature of Person Handling Over
		//Signature of Recipient
		
		// get nik name from sales and current user login
		$userID = $receipt->sales_id;
		$recipient = $this->mreceipt->getNikName($userID);
		$handler = $this->mreceipt->getNikName($this->session->userdata('username'));
		
		// signature field
		$this->fpdf->Ln( 30 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 55, 12, "Signature of Person Handling Over:", 1, 0, 'L', true );
		
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 60, 12, $handler, 1, 0, 'L', $fill );
		$this->fpdf->Cell( 40, 12, "", 1, 0, 'L', $fill );
		
		$this->fpdf->Ln( 12 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 55, 12, "Signature of Recipient:", 1, 0, 'L', true );	
		//$this->fpdf->Multicell(70,8,"Signature of Person Handling Over:\n$recipient", 1, 1, 'L', true );
		
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 60, 12, $recipient, 1, 0, 'L', $fill );
		$this->fpdf->Cell( 40, 12, "", 1, 0, 'L', $fill );
		//$this->fpdf->Multicell(70,8,"Signature of Person Handling Over:\n$recipient", 1, 1, 'L', true );
		
		
		
		//Position at 1.5 cm from bottom 
		$this->fpdf->SetY(-15); 
		$this->fpdf->Cell(0, 8, "~~ Printed by " . $this->session->userdata('username') . " ~~", 0, 0, 'C' );
		$this->fpdf->SetFont('Arial','I',8); 
		//Page number 
		$this->fpdf->Cell(10,8,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'R'); 
		
		
		
		//cetak ke file pdf
		$this->fpdf->Output();
	
	}



	
}